<?php
/*
 * @Author: an
 * @Date: 2021/12/17
 * @Description: 应用管理
 */


namespace App\Services\Aliyun\AudioAndVideo;

use AlibabaCloud\SDK\Rtc\V20180111\Models\DescribeAppsRequest;
use AlibabaCloud\SDK\Rtc\V20180111\Models\ModifyAppRequest;
use AlibabaCloud\SDK\Rtc\V20180111\Rtc;
use App\Services\Aliyun\BaseService;
use App\Services\Configs\DefaultService;
use Darabonba\OpenApi\Models\Config;

class AppManageService extends BaseService
{
    private static $accessKeyId = '';
    private static $accessKeySecret = '';

    public function __construct()
    {
        $config = $this->getConfig(DefaultService::ALI_AV);
        self::$accessKeyId = $config['accessKeyId'];
        self::$accessKeySecret = $config['accessKeySecret'];
    }

    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Rtc Client
     */
    public function createClient($accessKeyId, $accessKeySecret)
    {
        $config = new Config([
            "accessKeyId" => self::$accessKeyId,
            "accessKeySecret" => self::$accessKeySecret
        ]);
        // 访问的域名
        $config->endpoint = "rtc.aliyuncs.com";
        return new Rtc($config);
    }

    /**
     * 查询应用列表
     * @param null $appId 非必填 应用ID，通过控制台创建和查询，仅支持传单个ID。参数为空表示查询所有应用ID。
     * @param null $status 非必填 应用状态，参数为空表示查询所有状态。取值：1：可用。2：停用。3：欠费停用。
     * @param null $order 非必填 排序方式。取值：asc：递增。desc（默认值）：递减。
     * @param null $pageNum 非必填 第几页，默认查询第1页。
     * @param null $pageSize 非必填 每页显示个数，默认为10。
     * @return \AlibabaCloud\SDK\Rtc\V20180111\Models\DescribeAppsResponse|string
     * @author an
     * @time 2021-12-17 16:03
     */
    public function describeApps($appId = null, $status = null, $order = null, $pageNum = 1, $pageSize = 10)
    {
        try {
            $client = self::createClient(self::$accessKeyId, self::$accessKeySecret);
            $describeAppsRequest = new DescribeAppsRequest([
                "appId" => $appId,
                "status" => $status,
                "order" => $order,
                "pageNum" => $pageNum,
                "pageSize" => $pageSize
            ]);
            // 复制代码运行请自行打印 API 的返回值
            $data = $client->describeApps($describeAppsRequest);
            return ['code' => 0, 'msg' => 'ok', 'data' => $data->body];
        } catch (\Exception $exception) {
            return ['code' => 1, 'msg' => $exception->getMessage()];
        }
    }

    //修改指定应用的名称
    public function modifyApp($appId, $appName)
    {
        try {
            $client = self::createClient(self::$accessKeyId, self::$accessKeySecret);
            $modifyAppRequest = new ModifyAppRequest([
                "appId" => $appId,
                "appName" => $appName
            ]);
            // 复制代码运行请自行打印 API 的返回值
            $data = $client->modifyApp($modifyAppRequest);
            return ['code' => 0, 'msg' => 'ok', 'data' => $data->body];
        } catch (\Exception $exception) {
            return ['code' => 1, 'msg' => $exception->getMessage()];
        }
    }
}
